package com.common.www.config;

import com.common.www.aop.resolver.SysUserArgumentResolver;
import com.common.www.filter.HttpInterceptor;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.Charset;
import java.util.List;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    private static final Logger logger = LoggerFactory.getLogger(WebConfig.class);
    /**
     * 账户信息
     */
    public static String ACCOUNT_KEY = "ACCOUNT_KEY";
    /**
     * 授权信息
     */
    public static String Token = "token";
    /**
     * 上次访问的网页
     */
    public static String LastUrl = "LastUrl";

    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/api/auth/user/login"//登录接口
                        , "/api/auth/user/exitLogin"//退出登录接口
                        , "/api/auth/user/refreshToken"//刷新token不拦截
                        , "/api/auth/user/loginByWxCode"//三方登录接口不拦截
                        , "/api/auth/user/loginByWxApp"//三方登录接口不拦截
                        , "/api/auth/user/getWebAccessToken"//三方登录接口不拦截
                        , "/api/auth/user/refreshUserToken"//三方登录接口不拦截
                        , "/api/auth/user/updateByUserId"//三方登录后更新用户信息
                        , "/api/auth/user/loginByWxCodePhone"//三方登录后更新用户信息
                        , "/login/**"
                        , "/mgt/session/**"
                        , "/**/*.js", "/**/*.css"

                        , "/error/**"
                        , "/test/**"
                        , "/api/auth/user/getUserBySession"
                        , "/api/medical/**"
                        , "/api/test/**"
                        /**导出**/
                        , "/mgt/analyseChiCun/export**"
                        , "/mgt/allTabulation/exportQxqd"
                        , "/mgt/innerBidBaseTabulation/exportInnerBd"
                        , "/mgt/outerBidBaseTabulation/exportOuterBd"
                        , "/mgt/innerTabulation/exportInnerQd"
                        , "/mgt/outerTabulation/exportOuterQd"
                        , "/mgt/statistical/exportZxdb"
                        , "/mgt/statistical/exportZhdj"
                        , "/mgt/contrastScheme/exportScheme"
                        , "/mgt/precipitateDb/exportDb"
                        , "/mgt/precipitateSsdb/exportSsdb"
                        , "/mgt/xfgcBidBaseTabulation/exportXfgcBd"
                        , "/mgt/xfgcTabulation/exportXfgcQd"
                        , "/mgt/statisticalXfgc/exportZhdb"
                        , "/mgt/statisticalXfgc/exportHxdb"
                        , "/mgt/xfgcPrecipitate/exportDb"
                        , "/mgt/statistical/exportRankingProcess"
                        //文件动态下载
                        , "/api/file/downloadFile"
                        //临时文件下载
                        , "/temp/**"
                        , "/version/**"
                        , "/verify/**"
                        , "/head/**"
                        , "/common/**"
                        , "/qianMing/**"
                        //版本号查询
                        , "/api/version/**"
                        , "/api/device/sendQrCode"
                        , "/api/devicestation/**"
                        , "/api/device/**"
                        , "/price/**"

//                        , "/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**"
                );
    }

    @Bean
    public HttpInterceptor getInterceptor() {
        return new HttpInterceptor();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
//    /**
//     * Cors跨域配置
//     * @param registry
//     */
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**").allowedMethods("*").allowedOrigins("*").allowedHeaders("*");
//    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder() { //密码加密
        return new BCryptPasswordEncoder(4);
    }

    //解决中文乱码问题
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //解决中文乱码
        converters.add(responseBodyConverter());
        //解决 添加解决中文乱码后 上述配置之后，返回json数据直接报错 500：no convertter for return value of type
        converters.add(messageConverter());

    }


    @Bean
    public HttpMessageConverter<String> responseBodyConverter() {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        return converter;
    }

    @Bean
    public MappingJackson2HttpMessageConverter messageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(getObjectMapper());
        return converter;
    }

    @Bean
    public ObjectMapper getObjectMapper() {
        return new ObjectMapper();
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(currentUserMethodArgumentResolver());
    }

    /**
     * 参数解析
     *
     * @return MallUserArgumentResolver
     */
    @Bean
    public SysUserArgumentResolver currentUserMethodArgumentResolver() {
        return new SysUserArgumentResolver();
    }
}
